<?php

$mcrp = './';
include_once('functions/session.php');
include_once('skin/default.php');
include_once(MakeLangPath() . 'register.php');

$SkinTitle = $lang['SkinTitle'];

include('skin/header.php');

if($mcuser->id != 0 && $mcuser->isadmin == 0)
{
?>
	<div class="mc_box">
		<h3><?php echo $lang['title_sinscrire']; ?></h3><br />
		<center><?php echo $lang['deja_enregistre']; ?></center><br />
	</div>
<?php
}
else if(isset($_GET['valid']) && isset($_GET['id']))
{
	if(is_numeric($_GET['valid']) && is_numeric($_GET['id']))
	{
		$ui = GetUserById((int)$_GET['id']);
		if($ui === false)
		{
		?>
			<div class="mc_box">
				<h3><?php echo $lang['title_sinscrire']; ?></h3>
				<center><?php echo $lang['erreur_validation_user']; ?></center>
			</div>
		<?php
		}
		else if($ui->valid == 0)
		{
		?>
			<div class="mc_box">
				<h3><?php echo $lang['title_sinscrire']; ?></h3>
				<center><?php echo $lang['erreur_validation_dejaactive']; ?></center>
			</div>
		<?php
		}
		else if($ui->valid == (int)$_GET['valid'])
		{
			ValidateUser($ui);
			ComputeContestsPoints();
			?>
			<div class="mc_box">
				<h3><?php echo $lang['title_sinscrire']; ?></h3>
				<p>
				<center><?php echo $lang['inscription_ok']; ?></center>
				</p>
			</div>
			<?php
		}
		else
		{
		?>
			<div class="mc_box">
				<h3><?php echo $lang['title_sinscrire']; ?></h3>
				<p><center><?php echo $lang['erreur_validation_num']; ?></center></p>
			</div>
		<?php
		}
	}
	else
	{
	?>
		<div class="mc_box">
			<h3><?php echo $lang['title_sinscrire']; ?></h3>
			<p><center><?php echo $lang['erreur_validation_lien']; ?></center></p>
		</div>
	<?php
	}
}
else
{
	$DisplayForm = true;
	$Error = false;
	if(isset($_POST['posted']))
	{
		if($_POST['username'] == null || $_POST['sha1'] == null || $_POST['sha1_confirm'] == null || $_POST['email'] == null || $_POST['email_confirm'] == null)
			$Error = $lang['erreur_mail_champs'];
		else if($_POST['sha1'] != $_POST['sha1_confirm'])
			$Error = $lang['erreur_mail_pass'];
		else if($_POST['email'] != $_POST['email_confirm'])
			$Error = $lang['erreur_mail_mail'];
		else if(!preg_match('#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#', $_POST['email']))
			$Error = $lang['erreur_mail_invalidmail'];
		else if(IsUsernameExisting($_POST['username']))
			$Error = $lang['erreur_mail_user'];
		else
		{
			$UserInfos = new UserInfos;
			$UserInfos->name = htmlspecialchars($_POST['username']);
			$UserInfos->password = $_POST['sha1'];
			$UserInfos->email = $_POST['email'];
			if(CreateUser($UserInfos))
			{
			?>
				<div class="mc_box">
					<h3><?php echo $lang['title_sinscrire']; ?></h3>
					<div class="mc_box_content">
					<p>
					<?php echo $lang['ok_need_validation']; ?>
					</p>
					</div>
				</div>
			<?php
				$DisplayForm = false;
			}
			else
			{
				$Error = $lang['erreur'];
			}
		}
	}


	if($DisplayForm)
	{
	?>
	<div class="mc_box">
		<h3><?php echo $lang['title_sinscrire']; ?></h3>
		<div class="mc_box_content">
			<center>
			<?php if($Error != null) echo "<font color=\"red\"><b>$Error</b></font>"; ?>
			<form method="post">
			  <table>
				 <tr>
					<td align="right"><?php echo $lang['form_user']; ?></td>
					<td><input type="text" class="mc_input_box" tabindex="6" name="username" size="25" /></td>
				 </tr>
				 <tr>
					<td align="right"><?php echo $lang['form_pass']; ?></td>
					<td><input type="password" id="mc_reg_password" class="mc_input_box" tabindex="7" name="password" size="25" />
						<input type="hidden" name="sha1" id="mc_reg_sha1" /></td>
				 </tr>
				 <tr>
					<td align="right"><?php echo $lang['form_pass2']; ?></td>
					<td><input type="password" id="mc_password_confirm" class="mc_input_box" tabindex="8" name="password_confirm" size="25" />
						<input type="hidden" name="sha1_confirm" id="mc_sha1_confirm" /></td>
				 </tr>
				 <tr>
					<td align="right"><?php echo $lang['form_mail']; ?></td>
					<td><input type="text" class="mc_input_box" tabindex="9" name="email" size="25" maxlength="100" /></td>
				 </tr>
				 <tr>
					<td align="right"><?php echo $lang['form_mail2']; ?></td>
					<td><input type="text" class="mc_input_box" tabindex="10" name="email_confirm" size="25" maxlength="100" /></td>
				 </tr>
				 <tr>
					<td id="mc_register_button"><input type="submit" name="submit" tabindex="11" id ="submit" value="<?php echo $lang['send']; ?>" onclick="RegisterHash();" /></td>
				 </tr>
			  </table>
			  <input type="hidden" name="posted" value="1" />
			</form>
			</center>
		</div>
	</div>
	<?php
	}
}

include('skin/footer.php');

?>
